import java.util.ArrayList;
import java.util.List;

public class Solution144 {

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> list=new ArrayList<>();
        dfs(list,root);
        return list;
    }

    void dfs(List<Integer> ans,TreeNode treeNode){
        if (treeNode==null){
            return;
        }
        ans.add(treeNode.val);
        dfs(ans,treeNode.left);
        dfs(ans,treeNode.right);
    }

    public static void main(String[] args) {
        System.out.println(new Solution144().preorderTraversal(new TreeNode(1,null,new TreeNode(2,new TreeNode(3),null))));
    }
}
